package LeetCode.stack_queue;

import java.util.Stack;

public class LC_150 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        int res = 0;
        for (int i = 0; i < tokens.length; i++) {
            if(!tokens[i].equals("+") && !tokens[i].equals("-")
                    && !tokens[i].equals("*") && !tokens[i].equals("/")){
                stack.push(Integer.parseInt(tokens[i]));
            } else {
                int i2 = stack.pop();
                int i1 = stack.pop();
                switch (tokens[i]){
                    case "+" :{
                        res = i1 + i2;
                        stack.push(res);
                        break;
                    }
                    case "-" :{
                        res = i1 - i2;
                        stack.push(res);
                        break;
                    }
                    case "*" :{
                        res = i1 * i2;
                        stack.push(res);
                        break;
                    }
                    case "/" :{
                        res = i1 / i2;
                        stack.push(res);
                        break;
                    }
                }
            }
        }
        return stack.pop();
    }
}
